package com.heitian.ssm.controller;

import com.alibaba.fastjson.JSON;
import com.heitian.ssm.Constants.WebConstants;
import com.heitian.ssm.model.AdminEntity;
import com.heitian.ssm.model.HelpEntity;
import com.heitian.ssm.model.User;
import com.heitian.ssm.service.HelpService;
import com.heitian.ssm.utils.*;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: mishuai
 * Date: 2018-04-23
 * Time: 15:18
 * Des:
 */
@Controller
@RequestMapping("/adminShower")
public class HelpController {
    private Logger log = Logger.getLogger(AdminLoginController.class);
    @Resource
    private HelpService helpService;

    @RequestMapping("/insertHelp")
    @ResponseBody
    public void intsertHelp(HttpServletRequest request, HttpServletResponse response){
        String title1=request.getParameter("title1");
        String title2=request.getParameter("title2");
        String text=request.getParameter("text1");
        String adminUser=request.getParameter("adminUser");


        log.info("查询所有用户信息入参"+title1+title2+text);

        HelpEntity helpEntity=new HelpEntity();
        helpEntity.setHelpContent(text);
        helpEntity.setHelpTitle(title1);
        helpEntity.setHelpSubtitle(title2);
        helpEntity.setHelpUser(adminUser);
        helpEntity.setHelpModifyUser(adminUser);
        byte by=(byte)1;
        helpEntity.setIsStatus(by);
        helpEntity.setCreateTime(new Date());
        helpEntity.setModifyTime(new Date());

   
        int i=0;
        try {
            i=helpService.insertContent(helpEntity);
        }catch (Exception e){
            log.error(e);
        }

        if(i<=0){
            JsonResultUtils.ajaxReturn(response, FastJsonUtils.toJSONString(Result.falseReturnCodeMsg("F_99999","插入数据失败")));
        }else {
           
            JsonResultUtils.ajaxReturn(response,FastJsonUtils.toJSONString(Result.successReturn(helpEntity)));
        }
    }

    @RequestMapping("/insertContent")
    @ResponseBody
    public void intsertContent(HttpServletRequest request, HttpServletResponse response){
        String title1=request.getParameter("title1");
        String title2=request.getParameter("title2");
        String text=request.getParameter("text1");
        String adminUser=request.getParameter("adminUser");


        log.info("查询所有用户信息入参"+title1+title2+text);

        HelpEntity helpEntity=new HelpEntity();
        helpEntity.setHelpContent(text);
        helpEntity.setHelpTitle(title1);
        helpEntity.setHelpSubtitle(title2);
        helpEntity.setHelpUser(adminUser);
        helpEntity.setHelpModifyUser(adminUser);
        byte by=(byte)2;
        helpEntity.setIsStatus(by);
        helpEntity.setCreateTime(new Date());
        helpEntity.setModifyTime(new Date());


        int i=0;
        try {
            i=helpService.insertContent(helpEntity);
        }catch (Exception e){
            log.error(e);
        }

        if(i<=0){
            JsonResultUtils.ajaxReturn(response, FastJsonUtils.toJSONString(Result.falseReturnCodeMsg("F_99999","插入数据失败")));
        }else {

            JsonResultUtils.ajaxReturn(response,FastJsonUtils.toJSONString(Result.successReturn(helpEntity)));
        }
    }

    @RequestMapping("/showContent")
    public String showUser(HttpServletRequest request,HttpServletResponse response, Model model){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        log.info("查询所有用户信息");
       HelpEntity helpEntity = helpService.selectContent();
        log.info(JSON.toJSONString(helpEntity));
        String modifyDate=sdf.format(helpEntity.getModifyTime()) ;
         model.addAttribute("helpEntity",helpEntity);
        model.addAttribute("modifyDate",modifyDate);
         return "article";
    }

    @RequestMapping("/showHelp")
    public String showHelp(HttpServletRequest request,HttpServletResponse response, Model model){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        log.info("查询所有用户信息");
        HelpEntity helpEntity = helpService.selectHelp();
        log.info(JSON.toJSONString(helpEntity));
        String modifyDate=sdf.format(helpEntity.getModifyTime()) ;
        model.addAttribute("helpEntity",helpEntity);
        model.addAttribute("modifyDate",modifyDate);
        return "articleList";
    }


    @RequestMapping("/indexContent")
    @ResponseBody
    public void indexContent(HttpServletRequest request, HttpServletResponse response) {
        log.info("查询所有用户信息主页");
        HelpEntity helpEntity = helpService.selectHelp();
        if (helpEntity == null) {
            JsonResultUtils.ajaxReturn(response, FastJsonUtils.toJSONString(Result.falseReturnCodeMsg("F_99999", "插入数据失败")));
        } else {

            JsonResultUtils.ajaxReturn(response, FastJsonUtils.toJSONString(Result.successReturn(helpEntity)));
        }
    }

}
